<?php

/**
 * 阿里房源信息
 */

namespace app\crontab\model;

use app\common\lib\RespCode;
use app\common\model\BaseModel;
use think\Exception;

class HouseAli extends BaseModel {

    /**
     * 修改房源纪录
     * @param $params
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit(&$params) {
//        $data = no_array_by_keys($params, 'id,day_itime,month_itime,house_type,source_type');

        // 获取该房源纪录
        $old = $this->where(['source_id' => $params['source_id']])->find();
        if (!empty($old)) {
            // 存在修改房源纪录
            if ($this->allowField(true)->isUpdate(true)->save($params, ['id' => $old->id])) {
                return true;
            }
            throw new Exception('修改房源纪录失敗', RespCode::PARAMS_ERR);
        }

        // 不存在添加房源纪录
        $params['add_house'] = 1;
        if (!$this->strict(false)->insert($params)) {
            throw new Exception('添加房源纪录失敗', RespCode::PARAMS_ERR);
        }
        return true;
    }

}